<?php

class Admin extends CI_Controller {

    var $READ = 1;
    var $WRITE = 2;
    var $UPDATE = 3;
    var $DELETE = 4;

    function __construct() {


        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session', "table", "pagination", 'form_validation'));


        if ($this->session->userdata("admin") === false) {
            $func = $this->uri->segment(2);
            if ($func == "applicants" || $func == "deleteApplicant") {
                redirect("admin/index/You must login as an admin first");
            }
        }


        $this->load->model("settings/admins_model", "", TRUE);
        $this->load->model("consolidate_model", "", TRUE);
        $this->load->model("applicants_model", "", TRUE);
        $this->load->model("settings/kim_model", "", TRUE);
        $this->load->model("settings/log_model", "", TRUE);
        $this->template = array(
            'table_open' => '<table style=\'border:5px;border-style:solid;width: 100%;\'>',
            'heading_row_start' => '<tr style=\'background-color:#ccccff;text-transform:capitalize\'>',
            'row_start' => '<tr style="background-color:#eeefff;">',
            'row_alt_start' => '<tr style="background-color:#deefff;">'
        );
        $this->table->set_template($this->template);
        $this->myConfig['uri_segment'] = 2;
        $this->myConfig['per_page'] = '100';

        $this->const['hits'] = $this->kim_model->getHitCount();
    }

    function index($errors="") {
        $data["title"] = "- Admin";
        $this->load->view('templates/header', $data);
        $data['errors'] = urldecode($errors);
        $this->load->view('admin/login', $data);
        $this->load->view('templates/footer');
    }

    function login() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|md5'); //md5
        $data["title"] = "- Logging in";
        $data['errors'] = "";
        $this->load->view('templates/header', $data);
        if ($this->form_validation->run() == FALSE) {
            $this->load->view("admin/login", $data);
        } else {
            if ($this->admins_model->validateLogin()) {
                $type = $this->admins_model->getProfile($this->input->post("username"))->admin_type;
                $data = array("username" => $this->input->post("username"), "admin" => $type);
                $this->session->set_userdata($data);
                $this->log_model->newLog("Admin Login", "Info", $this->input->post("username") . " logged in", $this->session->userdata("username"));
                redirect("admin_mis");
            } else {
                $data['errors'] = "Wrong username or password";
                $this->load->view('admin/login', $data);
                $this->log_model->newLog("Admin Login", "Info", $this->input->post("username") . " failed logged in", "System");
            }
        }
        $this->load->view('templates/footer');
    }

    function applicants($message="") {
        $query = $this->consolidate_model->getApplicants();
        $temp = array();
        foreach ($query as $row) {
            unset($row['date_of_employment']);
            unset($row['pay_roll_no']);
            unset($row['date_last_login']);
            unset($row['date_registered']);
            $username = $row["username"];
            $row["link"] = "<a href='javascript:loadUrl(\"#app_prof\",\"" . base_url() . "index.php/admin_mis/applicantProfile/$username\")'>More details..</a>
                <a href='javascript:gotoUrl(\"" . base_url() . "index.php/admin/deleteApplicant/" . $row['tid'] . "\",\"Delete applicant\")'>Delete Applicant</a>";
            unset($row['tid']);
            $temp[] = $row;
        }
        $query = $temp;
        $this->myConfig['total_rows'] = count($query);
        $this->myConfig['base_url'] = base_url() . "index.php/admin/applicants";
        $this->pagination->initialize($this->myConfig);
        $data['pagina'] = $this->pagination->create_links();
        $this->table->set_heading(array('Surname', 'First Name', 'Other Names', 'Username', "Department", 'Phone', 'Email', "ID No", "More details"));
        $data['tablet'] = $this->table->generate($query);

        $data["title"] = "- Applicants";
        $data["wide"] = TRUE;
        $this->load->view('templates/header', $data);
        $data['message'] = urldecode($message);
        $this->load->view("admin/applicants", $data);
        $this->load->view('templates/footer', $this->const);
    }

    function deleteApplicant($tid) {
        $query = $this->consolidate_model->getApplicants(NULL, $tid);
        if (count($query) > 0) {
            $names = $query[0]['surname'] . " " . $query[0]['first_name'] . " " . $query[0]['other_names'];
            $this->applicants_model->deleteApplicant($tid);
            redirect("admin/applicants/$names deleted");
        } else {
            redirect("admin/applicants/user not found"); //must never happen
        }
    }

}

?>
